using System;
using System.Windows.Forms.Design;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
//using VSLangProj2;
//using VSLangProj80;

namespace ProjectBase.Guidance.Actions
{
    /// <summary>
    /// Adds a solution folder item.
    /// </summary>
    [ServiceDependency(typeof (DTE))]
    public class AddSolutionFolderAction : ConfigurableAction
    {
        #region Input Properties

        private string destinationSolutionFolderName;
        private string solutionFolderName;

        [Input(Required = true)]
        public string NewSolutionFolderName
        {
            get { return solutionFolderName; }
            set { solutionFolderName = value; }
        }

        [Input(Required = false)]
        public string DestinationSolutionFolderName
        {
            get { return destinationSolutionFolderName; }
            set { destinationSolutionFolderName = value; }
        }

        #endregion

        #region ConfigurableAction implementation

        public override void Execute()
        {
            var vs = GetService<DTE>(true);
            var uiService = (IUIService) GetService(typeof (IUIService));

            try
            {
                var sln = (Solution2) vs.Solution;
                if (destinationSolutionFolderName == null)
                {
                    if (DteHelper.FindSolutionFolderByPath(vs.Solution, solutionFolderName) == null)
                    {
                        sln.AddSolutionFolder(solutionFolderName);
                    }
                }
                else
                {
                    Project proj = DteHelper.FindProjectByName(vs, destinationSolutionFolderName);
                    var folder = (SolutionFolder) proj.Object;
                    folder.AddSolutionFolder(solutionFolderName);
                }
            }
            catch (Exception)
            {
                uiService.ShowMessage(string.Format("Failed to create solution folder '{0}' in solution '{1}'",
                                                    solutionFolderName, destinationSolutionFolderName));
                throw;
            }
        }

        public override void Undo()
        {
        }

        #endregion
    }
}